package tech.threekilogram.activitytaskstacktest;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

      @Override
      protected void onCreate ( Bundle savedInstanceState ) {

            super.onCreate( savedInstanceState );
            setContentView( R.layout.activity_main );
      }

      public void toDetail ( View view ) {

            /* 直接启动不会有parentActivity效果 */
            DetailActivity.start( this );
      }

      public void taskBuilder ( View view ) {

            Intent intent = new Intent( this, DetailActivity.class );
            intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP );

            TaskStackBuilder taskStackBuilder = TaskStackBuilder.create( this );
            taskStackBuilder.addParentStack( DetailActivity.class );
            taskStackBuilder.addNextIntent( intent );

            taskStackBuilder.startActivities();
      }

      private void createNotification ( TaskStackBuilder taskStackBuilder ) {

            PendingIntent pendingIntent = taskStackBuilder
                .getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT );

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.ic_launcher_foreground )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a TaskStack notification " )
                .setAutoCancel( true )
                .setContentIntent( pendingIntent )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 12, notification );
      }

      private String getChannel ( ) {

            if( VERSION.SDK_INT > VERSION_CODES.O ) {

                  int importance = NotificationManager.IMPORTANCE_DEFAULT;
                  /* 必须设置 importance */
                  NotificationChannel channel = new NotificationChannel(
                      "CHANNEL", "CHANNEL_NAME", importance );
                  channel.setDescription( "this is test notification channel" );
                  NotificationManager notificationManager = getSystemService(
                      NotificationManager.class );
                  assert notificationManager != null;
                  notificationManager.createNotificationChannel( channel );
                  return "CHANNEL";
            } else {
                  return "CHANNEL";
            }
      }

      public void notification ( View view ) {

            Intent intent = new Intent( this, DetailActivity.class );
            intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP );

            TaskStackBuilder taskStackBuilder = TaskStackBuilder.create( this );
            taskStackBuilder.addParentStack( DetailActivity.class );
            taskStackBuilder.addNextIntent( intent );

            createNotification( taskStackBuilder );
      }
}
